package  
{
	
	import net.flashpunk.Entity;
	import net.flashpunk.FP;
	import net.flashpunk.graphics.Image;
	import net.flashpunk.utils.Data;
	import net.flashpunk.utils.Draw;
	import net.flashpunk.graphics.Spritemap;
	
	/**
	 * ...
	 * @author Felix Fleiß, Martina Rasch
	 */
	public class Obstacle extends Entity
	{
		//embed image
		[Embed(source = 'res/meteorit.png')] 
		private const OBSTACLE:Class;
		private var startpos:Number;
		//CHANGE MIN/MAX VALUES OF OBSTACLES HERE!!!
		private var speed:Number;
		public var screen_width:int;
		public var screen_height:int;
		private static var obstacleNumber:int;
		private var crashTime:Number = 0;
		private var dying:Boolean = false;
		
		private var obstacle:Spritemap = new Spritemap(OBSTACLE, 100, 50);
		
		public function Obstacle() 
		{
			screen_width = Data.readInt("screen_width");
			screen_height = Data.readInt("screen_height");
			//CHANGE X AND Y COORDS OF OBSTACLES HERE!!!
			speed = randomNumber(Number(Data.readString("obstacle_min_speed")), Number(Data.readString("obstacle_max_speed")));
			x = screen_width;
			obstacleNumber++;
			if (obstacleNumber % 4 == 0)
				y = Player.s_y;
			else 
				y = randomNumber(0, Data.readInt("screen_height")-50);
			//draw obstacle on coord (x, y)
			graphic = obstacle;
			//set hitbox
			setHitbox(50, 50);
			//set type of collision
			type = "obstacle";
			
			obstacle.add("fly", [0, 1], 60, true);
			obstacle.add("split", [2], 60, true);
			obstacle.add("away", [3], 60, true);
		}
		
		//update
		override public function update():void 
		{
			if (!dying) {
				obstacle.play("fly");
				x -= FP.elapsed * speed;
				//done
				/*if (x <= 50) {
					
					Image(graphic).color = 0xFF0000;
				}*/
				//destroy when obstacle is out of the screen
				if (x <= 0)
				{
					FP.world.remove(this); 
				}
			} else {
				crashTime += FP.elapsed;
				if (crashTime < 0.2) {
					obstacle.play("split");
				} else if (crashTime < 0.4) {
					obstacle.play("away");
				} else {
					FP.world.remove(this); 
				}
			}
		}
		
		//remove
		public function destroy():void 
		{
			dying = true;
		}
		
		//function can be used to get a random number between min and max
		private function randomNumber(min:Number=0, max:Number=1):Number
		{
			return Math.floor(Math.random() * (max-min)) + min;
		}	
		
		public function getDead():Boolean {
			return dying;
		}
		
	}

}